/**
 * 1684. 统计一致字符串的数目
 * https://leetcode-cn.com/problems/count-the-number-of-consistent-strings/
 */
public class Solutions_1684 {
    public static void main(String[] args) {
//        String allowed = "ab";
//        String[] words = {"ad", "bd", "aaab", "baa", "badab"};  // output: 2

//        String allowed = "abc";
//        String[] words = {"a", "b", "c", "ab", "ac", "bc", "abc"};  // output: 7

        String allowed = "cad";
        String[] words = {"cc", "acd", "b", "ba", "bac", "bad", "ac", "d"};  // output: 4

        int result = countConsistentStrings(allowed, words);
        System.out.println(result);
    }

    /**
     * 1.记录下 allowed 中出现的字符
     *      map[0] = true：字符 'a' 在 allowed 中出现
     * 2.遍历 words，若 word 中全部字符都在 allowed 中存在，res ++
     */
    public static int countConsistentStrings(String allowed, String[] words) {
        boolean[] map = new boolean[26];
        char[] arr = allowed.toCharArray();
        for (char c : arr) {
            map[c - 'a'] = true;
        }
        int cnt = 0;
        for (boolean flag : map) {
            if (flag) {
                cnt ++;
            }
        }
        if (cnt == 26) {
            // allowed 中包含全部的小写字母
            return words.length;
        }
        int res = 0;
        loop:
            for (String word : words) {
                for (int i = 0; i < word.length(); i++) {
                    char c = word.charAt(i);
                    if (!map[c - 'a']) {
                        // 该字符在 allowed 中不存在
                        continue loop;
                    }
                }
                res ++;
            }
        return res;
    }
}
